#!/usr/bin/env bash

# export giteePullRequestIid=12
# export BUILD_DIR=/data/pr
# export giteeSourceBranch=docs-develop

set -e

conName="pr_"${giteePullRequestIid}
dir=${BUILD_DIR}/"pr_"${giteePullRequestIid}
cacheDir=${dir}/cache
publicDir=${dir}/publicDir
pr_port_file=${dir}/pr_port.txt

if [ ! -d ${cacheDir} ];then
  mkdir -p ${cacheDir}
fi

echo "giteePullRequestIid : ${giteePullRequestIid}"
echo "giteeTargetBranch   : ${giteeTargetBranch}"

# 删除文件
rm -rf .cache
rm -rf public
mkdir public

# 恢复文件
echo "Restore Cache as $(date '+%Y-%m-%d %H:%M:%S')"
if [ ! -d ${publicDir} ];then
    if [[ ${giteeTargetBranch} == "master" ]]; then
        rsync -rltgoD --delete /data/mogdb-docs/ public
    else
        echo "mogdb-beta-docs"
        rsync -rltgoD --delete /data/mogdb-beta-docs/ public
    fi
else
   rsync -rltgoD --delete ${publicDir}/ public
fi

mv ${cacheDir} .cache
